﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal_Tech;

namespace Portal_Tech
{
    class Distribution
    {
        public static List<SubGroup> Distribute(List<ParadeFloat> parade, List<SubGroup> groups, int numberOfFloats)
        {
            groups.Sort(delegate(SubGroup g1, SubGroup g2) { return g2.numberInGroup.CompareTo(g1.numberInGroup); });
            int count = groups.Count;
            int median = count / 2 - 1;
            List<SubGroup> added = new List<SubGroup>();
            List<SubGroup> notAdded = new List<SubGroup>();
            int average = 0;
            int sum = 0;
            foreach (SubGroup group in groups)
            {
                sum += group.numberInGroup;
            }
            average = sum / numberOfFloats;

            for (int i = median, j = median + 1; i > 0 && j < count; i--, j++)
            {
                int floatIndex = 1;
                foreach (ParadeFloat pfloat in parade)
                {
                    if (pfloat.numberOfRiders + groups[i].numberInGroup <= average && pfloat.numberOfRiders + groups[i].numberInGroup <= 40)
                    {
                        pfloat.groups.Add(groups[i]);
                        added.Add(groups[i]);
                        pfloat.numberOfRiders += groups[i].numberInGroup;
                        groups[i].SetFrontMiddleBack(floatIndex);
                        break;
                    }
                    floatIndex++;
                }
                floatIndex = 1;
                foreach (ParadeFloat pfloat in parade)
                {
                    if (pfloat.numberOfRiders + groups[j].numberInGroup <= average && pfloat.numberOfRiders + groups[i].numberInGroup <= 40)
                    {
                        pfloat.groups.Add(groups[j]);
                        added.Add(groups[j]);
                        pfloat.numberOfRiders += groups[j].numberInGroup;
                        groups[j].SetFrontMiddleBack(floatIndex);
                        break;
                    }
                    floatIndex++;
                }
            }
            foreach (SubGroup g in groups)
            {
                bool inAdded = false;
                foreach (SubGroup s in added)
                {
                    
                    if (g.GroupID == s.GroupID)
                        inAdded = true;
                }
                if (!inAdded)
                    notAdded.Add(g);
            }
            foreach (SubGroup g in notAdded)
            {
                int floatIndex = 1;
                foreach (ParadeFloat pfloat in parade)
                {
                    if (pfloat.numberOfRiders + g.numberInGroup <= 40)
                    {
                        pfloat.groups.Add(g);
                        added.Add(g);
                        //notAdded.Remove(g);
                        pfloat.numberOfRiders += g.numberInGroup;
                        g.SetFrontMiddleBack(floatIndex);
                        break;
                    }
                    floatIndex++;
                }
            }
            if (notAdded.Count != 0)
            {
                int notAddedSum = 0;
                foreach (SubGroup g in notAdded)
                {
                    notAddedSum += g.numberInGroup;
                }
            }
            return notAdded;

        }
    }
}
